let sortDirection = [true, true]; // true for ascending, false for descending  
  
function filterTable() {  
    let input, filter, table, tr, td, i, j, txtValue;  
    input = document.getElementById("searchInput");  
    filter = input.value.toUpperCase();  
    table = document.getElementById("companyTable");  
    tr = table.getElementsByTagName("tr");  
    for (i = 1; i < tr.length; i++) {  
        tr[i].style.display = "none"; // Hide all rows initially  
        td = tr[i].getElementsByTagName("td")[0]; // Get the first cell of each row  
        if (td) {  
            txtValue = td.textContent || td.innerText;  
            if (txtValue.toUpperCase().indexOf(filter) > -1) {  
                tr[i].style.display = ""; // Show the rows that match the search  
            }  
        }  
    }  
}  
  
function sortTable(n) {  
    let table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;  
    table = document.getElementById("companyTable");  
    switching = true;  
    dir = "asc"; // Set the sorting direction to ascending  
  
    // Make a loop that will continue until no switching has been done  
    while (switching) {  
        switching = false;  
        rows = table.rows;  
  
        // Loop through all table rows (except headers)  
        for (i = 1; i < (rows.length - 1); i++) {  
            shouldSwitch = false;  
  
            // Get the two elements to be compared  
            x = rows[i].getElementsByTagName("TD")[n];  
            y = rows[i + 1].getElementsByTagName("TD")[n];  
  
            // Check if the two rows should switch place  
            if (dir == "asc") {  
                if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {  
                    // If yes, mark as a switch and break the loop  
                    shouldSwitch = true;  
                    break;  
                }  
            } else if (dir == "desc") {  
                if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {  
                    // If yes, mark as a switch and break the loop  
                    shouldSwitch = true;  
                    break;  
                }  
            }  
        }  
        if (shouldSwitch) {  
            // Make the switch and mark that a switch has been done  
            rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);  
            switching = true;  
            switchcount++;  
        } else {  
            // If no switching has been done AND the direction is "asc", set the direction to "desc" and run the loop again.  
            if (switchcount == 0 && dir == "asc") {  
                dir = "desc";  
                switching = true;  
            }  
        }  
    }  
  
    // Toggle the sort direction for the next click  
    sortDirection[n] = !sortDirection[n];  
}